package com.chenyun.message.sms.service.client.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.chenyun.common.enums.SmsEnum;
import com.chenyun.message.sms.config.SmsConfig;
import com.chenyun.message.sms.service.client.ISmsClient;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.List;

/**
 * 短信工厂
 *
 * @author: Lonni
 * @date: 2023/12/6 0006 9:24
 */
@Scope(value = "prototype")
@Slf4j
public class SmsClientFactory implements Serializable {

    /**
     * 用于存储客户端集合
     * 租户id: 渠道id 客户端
     */
    private static final Table<String, String, ISmsClient> CLIENT_TABLES = HashBasedTable.create();

     public final static SmsClientFactory me=new SmsClientFactory();


    /**
     * 初始化短信客户端
     * @param configs
     */
    public void init(List<SmsConfig> configs) {
        if (CollectionUtil.isNotEmpty(configs)) {
            for (SmsConfig smsConfig : configs) {
               this.initConfig(smsConfig);
            }
        }
    }


    public void initConfig(SmsConfig smsConfig){
        ISmsClient iSmsClient = null;
        switch (smsConfig.getType()) {
            case ALIYUN:
                AliSmsClient aliSmsClient = new AliSmsClient();
                aliSmsClient.init(smsConfig);
                iSmsClient = aliSmsClient;
                break;
            case BAIDU:
                log.info("暂未实现百度云短信方式");
                break;
            case TENCENT:
                TencentSmsClient tencentSmsClient = new TencentSmsClient();
                tencentSmsClient.init(smsConfig);
                iSmsClient = tencentSmsClient;
                break;
            default:
                return;
        }
        CLIENT_TABLES.put(smsConfig.getTenantId(), smsConfig.getType().getCode(), iSmsClient);

    }




    /**
     * 获取客户端
     *
     * @param tid        租户id
     * @param smsCode 客户端类型
     * @return
     * @See SmsChannel表
     */
    public ISmsClient getClient(String tid, SmsEnum smsCode) {
       return CLIENT_TABLES.get(tid, smsCode.getCode());
    }




}



